package com.example.shiro.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private SecurityManager securityManager;

    @RequiresPermissions("sys:role:list")
    @GetMapping("role")
    public String role() {

        return "OK";
    }


    @GetMapping("login")
    public String login(String username, String password) {

        Subject subject = SecurityUtils.getSubject();
        assert !subject.isAuthenticated();

        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        subject.login(token);

        assert subject.isAuthenticated();

        subject.isPermitted("sys:role:list");
        subject.hasRole("r1");


        redisTemplate.opsForValue().set("hello", "world");

        return "OK";
    }
}
